<template>
  <div v-if="isAdd == 1" class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="标题" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入标题" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="治疗类别" prop="treatId">
            <el-select v-model="queryParams.treatId" placeholder="请选择治疗类别" clearable>
              <el-option v-for="dict in treat_cate" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="鸡苗批次" prop="hatchId">
            <el-input v-model="queryParams.hatchId" placeholder="请输入鸡苗批次" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="操作时间" prop="happenTime">
            <el-input v-model="queryParams.happenTime" placeholder="请输入操作时间" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="审核状态" prop="auditStatus">
            <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
              <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="审核时间" prop="auditTime">
            <el-date-picker v-model="queryParams.auditTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择审核时间" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card v-if="isAdd == 1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm/chicken:grow:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm/chicken:grow:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm/chicken:grow:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm/chicken:grow:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="growList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column label="标题" align="center" prop="orderNo" />
        <el-table-column label="治疗类别" align="center" prop="treatId">
          <template #default="scope">
            <dict-tag :options="treat_cate" :value="scope.row.treatId" />
          </template>
        </el-table-column>
        <el-table-column label="鸡苗批次" align="center" prop="hatchName" />
        <el-table-column label="日龄" align="center" prop="ageDay" />
        <el-table-column label="鸡群数量" align="center" prop="chickenNum" />
        <el-table-column label="药品金额" align="center" prop="totalMoney" />
        <el-table-column label="操作时间" align="center" prop="happenTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.happenTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="说明" prop="remark" show-overflow-tooltip width="200" />
        <el-table-column label="审核状态" align="center" prop="auditStatus">
          <template #default="scope">
            <dict-tag
              v-if="scope.row.auditStatus !== 4 && scope.row.auditStatus !== 2"
              :options="order_status"
              :value="scope.row.auditStatus"
              @click="handleAudit(scope.row)"
            />
            <dict-tag v-else :options="order_status" :value="scope.row.auditStatus" />
          </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="auditTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['farm/chicken:grow:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['farm/chicken:grow:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
  <!-- 审核对话框 -->
  <!--    <audit-dialog v-model="dialog.visible" :row-data="rowData" @load-list="getList" />-->
  <Audit v-model="dialog.visible" :row-data="rowData" @load-list="getList" @ok="sonData" />
  <!-- 新增/编辑 -->
  <GrowAdd v-if="isAdd == 2" :row-data="rowData" @close-add="closeAdd" @get-list="getList" />
</template>

<script setup name="Grow" lang="ts">
import { listGrow, getGrow, delGrow, auditChickenGrowApi } from '@/api/farm/chicken/grow';
import { GrowVO } from '@/api/farm/chicken/grow/types';
import GrowAdd from '@/views/farm/chicken/grow/add.vue';
import { parseTime } from '@/utils/ruoyi';
import Audit from '@/components/Audit/index.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { order_status, treat_cate } = toRefs<any>(proxy?.useDict('order_status', 'treat_cate'));

const growList = ref<GrowVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const rowData = ref({});
const queryFormRef = ref<ElFormInstance>();
const growFormRef = ref<ElFormInstance>();
const isAdd = ref<number>(1);
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    treatId: undefined,
    hatchId: undefined,
    happenTime: undefined,
    auditStatus: undefined,
    auditTime: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询成长记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listGrow(queryParams.value);
  growList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 表单重置 */
const reset = () => {
  growFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: GrowVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  isAdd.value = 2;
};
/*关闭新增*/
const closeAdd = (e) => {
  isAdd.value = e;
};
/** 修改按钮操作 */
const handleUpdate = async (row?: GrowVO) => {
  reset();
  isAdd.value = 2;
  const _id = row?.id || ids.value[0];
  const res = await getGrow(_id);
  rowData.value = res.data;
};
/*打开审核窗口*/
const handleAudit = (e) => {
  rowData.value = e;
  dialog.visible = true;
  dialog.title = '审核';
};
/*当子组件有值时执行审核*/
const sonData = (e) => {
  if (e) {
    const audit = async () => {
      let res = await auditChickenGrowApi(e);
      if (res.code === 200) {
        proxy?.$modal.msgSuccess('审核成功');
        await getList();
      }
    };
    audit();
  }
};
/** 删除按钮操作 */
const handleDelete = async (row?: GrowVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除成长记录编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delGrow(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'farm/chicken/grow/export',
    {
      ...queryParams.value
    },
    `grow_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
